﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.DataAccess;
using DataObject.Database;
using System.Web.Security;

public partial class Pages_Boat_CabinItem : System.Web.UI.Page
{
    public int? CabinId { get; set; }
    public string BoatId { get; set; }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Request["CabinId"] != null)
        {
            CabinId = int.Parse(Request["CabinId"].ToString());
        }
        else CabinId = null;
        if (Request["BoatId"] != null)
        {
            BoatId = Request["BoatId"].ToString();
        }
        else BoatId = string.Empty;
        if (!IsPostBack)
        {
            LoadData();
        }
    }
    void LoadData()
    {
        BoatEntity _BoatEntity = BoatDAO.CreateInstant().GetBoat(BoatId);
        txtBoat.Text = _BoatEntity.BoatName;
        if (CabinId != null)
        {
            CabinEntity item = CabinDAO.CreateInstant().GetCabin(CabinId.Value);
            if (item != null)
            {
                txtCabinNumber.Text = item.CabinNumber;
                txtDescription.Text = item.Description;
                txtFloor.Text = item.Floor.Value.ToString();
                txtMaxGuests.Text = item.MaxGuests.Value.ToString();
                cbActive.Checked = item.Active;
                cbForRent.Checked = item.ForRent;
            }
        }
    }
    CabinEntity GetCabinEntity()
    {
        if (BoatId.Equals(string.Empty))
            return null;

        CabinEntity item = null;
        if (CabinId != null)
        {
            item = CabinDAO.CreateInstant().GetCabin(CabinId.Value);
        }
        if (item == null)
        {
            item = new CabinEntity();
            item.BoatID = BoatId;
        }
        item.CabinNumber = txtCabinNumber.Text;
        item.Active = cbActive.Checked;
        item.Description = txtDescription.Text;
        item.Floor = int.Parse(txtFloor.Text);
        item.ForRent = cbForRent.Checked;
        item.MaxGuests = int.Parse(txtMaxGuests.Text);
        return item;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        CabinEntity item = GetCabinEntity();
        if (item != null)
        {
            if (CabinId != null)
                CabinDAO.CreateInstant().UpdateCabin(item);
            else
                CabinDAO.CreateInstant().Add(item);
            Response.Redirect("CabinList.aspx?BoatId=" + item.BoatID);
        }
    }

}
